package com.moxiao.yfjh.module.blind.common;

import com.moxiao.yfjh.framework.common.util.jpush.JgPushSendUrl;
import com.moxiao.yfjh.framework.common.util.jpush.JgPushSendVo;
import com.moxiao.yfjh.module.blind.controller.admin.equity.vo.MemberEquityCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.member.vo.BlindMemberUserUpdateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberLike.vo.MemberLikeCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSayHello.vo.MemberSayhelloCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageCreateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.moment.BlindMemberMomentDO;
import com.moxiao.yfjh.module.blind.service.notifymessage.BlindNotifyMessageService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/2/20 17:12
 **/
@Component
public class BlindNotifyMessageUtils {
    @Resource
    private JgPushSend jgPushSend;

    @Resource
    private BlindNotifyMessageService blindNotifyMessageService;

    /**
     * 喜欢我消息通知
     * @param createReqVO
     */
    public void createLikeMessage(MemberLikeCreateReqVO createReqVO){
        BlindNotifyMessageCreateReqVO blindNotifyMessageCreateReqVO = new BlindNotifyMessageCreateReqVO();
        blindNotifyMessageCreateReqVO.setUserId(createReqVO.getReceive());
        blindNotifyMessageCreateReqVO.setMessageType(NotifyMessageEnum.LIKE_ME.getType());
        blindNotifyMessageCreateReqVO.setContent(NotifyMessageEnum.LIKE_ME.getContext());
        String jumpUrl = NotifyMessageEnum.LIKE_ME.getJumpUrl();
        blindNotifyMessageCreateReqVO.setJumpUrl(String.format(jumpUrl,createReqVO.getIssue()));
        blindNotifyMessageCreateReqVO.setReadStatus(0);
        blindNotifyMessageCreateReqVO.setStatus((byte) 1);
        blindNotifyMessageCreateReqVO.setTitle("喜欢通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(createReqVO.getReceive()), blindNotifyMessageCreateReqVO.getContent(),
                "喜欢通知", "您有新的喜欢！", blindNotifyMessageCreateReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(blindNotifyMessageCreateReqVO);
    }


    /**
     * 打招呼消息通知
     * @param createReqVO
     */
    public void createSayHelloMessage(MemberSayhelloCreateReqVO createReqVO){
        BlindNotifyMessageCreateReqVO blindNotifyMessageCreateReqVO = new BlindNotifyMessageCreateReqVO();
        blindNotifyMessageCreateReqVO.setUserId(createReqVO.getReceive());
        blindNotifyMessageCreateReqVO.setMessageType(NotifyMessageEnum.SAY_HELLO.getType());
        blindNotifyMessageCreateReqVO.setContent(NotifyMessageEnum.SAY_HELLO.getContext());
        String jumpUrl = NotifyMessageEnum.SAY_HELLO.getJumpUrl();
        blindNotifyMessageCreateReqVO.setJumpUrl(String.format(jumpUrl,createReqVO.getIssue()));
        blindNotifyMessageCreateReqVO.setReadStatus(0);
        blindNotifyMessageCreateReqVO.setStatus((byte) 1);
        blindNotifyMessageCreateReqVO.setTitle("打招呼通知");
        blindNotifyMessageService.createNotifyMessage(blindNotifyMessageCreateReqVO);
    }


    /**
     * 强制匹配通知
     * @param createReqVO
     */
    public void createForceMatchMessage(MemberEquityCreateReqVO createReqVO,BlindMemberUserDO memberUser){
        BlindNotifyMessageCreateReqVO blindNotifyMessageCreateReqVO = new BlindNotifyMessageCreateReqVO();
        blindNotifyMessageCreateReqVO.setUserId(String.valueOf(createReqVO.getMatchedId()));
        blindNotifyMessageCreateReqVO.setMessageType(NotifyMessageEnum.FORCE_PAIRING.getType());
        String context = NotifyMessageEnum.FORCE_PAIRING.getContext();
        String format = String.format(context,memberUser.getNickname());
        blindNotifyMessageCreateReqVO.setContent(format);
        String jumpUrl = NotifyMessageEnum.FORCE_PAIRING.getJumpUrl();
        blindNotifyMessageCreateReqVO.setJumpUrl(String.format(jumpUrl,memberUser.getId()));
        blindNotifyMessageCreateReqVO.setReadStatus(0);
        blindNotifyMessageCreateReqVO.setStatus((byte) 1);
        blindNotifyMessageCreateReqVO.setTitle("强制匹配通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(createReqVO.getMatchedId()), blindNotifyMessageCreateReqVO.getContent(),
                "强制匹配通知", "您已被强制配对！", blindNotifyMessageCreateReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(blindNotifyMessageCreateReqVO);
    }




    /**
     * 头像审核通知
     * @param updateReqVO
     */
    public void createAvatarAuditMessage(BlindMemberUserUpdateReqVO updateReqVO){
        BlindNotifyMessageCreateReqVO createReqVO = new BlindNotifyMessageCreateReqVO();
        createReqVO.setUserId(String.valueOf(updateReqVO.getId()));
        createReqVO.setMessageType(NotifyMessageEnum.AVATAR_REVIEW.getType());
        createReqVO.setImgurl(updateReqVO.getAvatar());
        createReqVO.setContent(NotifyMessageEnum.AVATAR_REVIEW.getContext());
        String jumpUrl = NotifyMessageEnum.AVATAR_REVIEW.getJumpUrl();
        createReqVO.setJumpUrl(String.format(jumpUrl,updateReqVO.getId()));
        createReqVO.setReadStatus(0);
        createReqVO.setStatus((byte) 1);
        createReqVO.setTitle("系统通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(updateReqVO.getId()), createReqVO.getContent(),
                "系统通知", "您的头像没有通过审核！", createReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(createReqVO);
    }


    /**
     * 视频审核失败通知
     */

    public void createVideoAuditMessage(BlindMemberMomentDO blindMemberMomentDO) {
        BlindNotifyMessageCreateReqVO createReqVO = new BlindNotifyMessageCreateReqVO();
        createReqVO.setUserId(String.valueOf(blindMemberMomentDO.getUserId()));
        createReqVO.setMessageType(NotifyMessageEnum.VIDEO_REVIEW.getType());
//        createReqVO.setImgurl(blindMemberMomentDO.getVideoUrl());
        createReqVO.setContent(NotifyMessageEnum.VIDEO_REVIEW.getContext());
        String jumpUrl = NotifyMessageEnum.VIDEO_REVIEW.getJumpUrl();
        createReqVO.setJumpUrl(String.format(jumpUrl, blindMemberMomentDO.getId()));
        createReqVO.setReadStatus(0);
        createReqVO.setStatus((byte) 1);
        createReqVO.setTitle("系统通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(blindMemberMomentDO.getUserId()), createReqVO.getContent(),
                "系统通知", "您动态中的视频审核失败！", createReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(createReqVO);
    }


    /**
     * 人脸认证成功通知
     */

    public void createVerifySelfieMessage(Long userId) {
        BlindNotifyMessageCreateReqVO createReqVO = new BlindNotifyMessageCreateReqVO();
        createReqVO.setUserId(String.valueOf(userId));
        createReqVO.setMessageType(NotifyMessageEnum.MEMBER_USER_AUTH_VERIFY_SELFIE_SUCCESS.getType());
        createReqVO.setContent(NotifyMessageEnum.MEMBER_USER_AUTH_VERIFY_SELFIE_SUCCESS.getContext());
        String jumpUrl = NotifyMessageEnum.MEMBER_USER_AUTH_VERIFY_SELFIE_SUCCESS.getJumpUrl();
        createReqVO.setJumpUrl(String.format(jumpUrl, userId));
        createReqVO.setReadStatus(0);
        createReqVO.setStatus((byte) 1);
        createReqVO.setTitle("系统通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(userId), createReqVO.getContent(),
                "系统通知", "人脸实名认证成功", createReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(createReqVO);
    }

    /**
     * 人脸认证成功通知
     */

    public void createVerifySelfieErrorMessage(Long userId) {
        BlindNotifyMessageCreateReqVO createReqVO = new BlindNotifyMessageCreateReqVO();
        createReqVO.setUserId(String.valueOf(userId));
        createReqVO.setMessageType(NotifyMessageEnum.MEMBER_USER_AUTH_VERIFY_SELFIE_ERROR.getType());
        createReqVO.setContent(NotifyMessageEnum.MEMBER_USER_AUTH_VERIFY_SELFIE_ERROR.getContext());
        String jumpUrl = NotifyMessageEnum.MEMBER_USER_AUTH_VERIFY_SELFIE_ERROR.getJumpUrl();
        createReqVO.setJumpUrl(String.format(jumpUrl, userId));
        createReqVO.setReadStatus(0);
        createReqVO.setStatus((byte) 1);
        createReqVO.setTitle("系统通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(userId), createReqVO.getContent(),
                "系统通知", "您的人脸实名认证失败，请明天再试", createReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(createReqVO);
    }

    /**
     * 用户昵称消息通知
     */
    public void createNicknameMessage(Long userId) {
        BlindNotifyMessageCreateReqVO createReqVO = new BlindNotifyMessageCreateReqVO();
        createReqVO.setUserId(String.valueOf(userId));
        createReqVO.setMessageType(NotifyMessageEnum.MEMBER_USER_NICKNAME_SUCCESS.getType());
        createReqVO.setContent(NotifyMessageEnum.MEMBER_USER_NICKNAME_SUCCESS.getContext());
        String jumpUrl = NotifyMessageEnum.MEMBER_USER_NICKNAME_SUCCESS.getJumpUrl();
        createReqVO.setJumpUrl(String.format(jumpUrl, userId));
        createReqVO.setReadStatus(0);
        createReqVO.setStatus((byte) 1);
        createReqVO.setTitle("系统通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(userId), createReqVO.getContent(),
                "系统通知", "您的昵称审核失败", createReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(createReqVO);
    }

    /**
     * 用户昵称消息通知
     */
    public void createSpeechMessage(Long userId) {
        BlindNotifyMessageCreateReqVO createReqVO = new BlindNotifyMessageCreateReqVO();
        createReqVO.setUserId(String.valueOf(userId));
        createReqVO.setMessageType(NotifyMessageEnum.MEMBER_USER_SPEECH_SUCCESS.getType());
        createReqVO.setContent(NotifyMessageEnum.MEMBER_USER_SPEECH_SUCCESS.getContext());
        String jumpUrl = NotifyMessageEnum.MEMBER_USER_SPEECH_SUCCESS.getJumpUrl();
        createReqVO.setJumpUrl(String.format(jumpUrl, userId));
        createReqVO.setReadStatus(0);
        createReqVO.setStatus((byte) 1);
        createReqVO.setTitle("系统通知");
        // 极光推送
        JgPushSendVo jgPushSendVo = getJgPushSendVo(String.valueOf(userId), createReqVO.getContent(),
                "系统通知", "您的星座测试已完成", createReqVO.getJumpUrl());
        jgPushSend.sendToAllAndroid(jgPushSendVo);
        blindNotifyMessageService.createNotifyMessage(createReqVO);
    }

    /**
     * 拼接极光推送
     */
    private @NotNull JgPushSendVo getJgPushSendVo(String userid, String content, String title, String msgTitle, String url) {
        JgPushSendVo jgPushSendVo = new JgPushSendVo();
        List<String> alias = new ArrayList<>();
        alias.add(userid);
        jgPushSendVo.setContent(jgPushSend.getStr(content));
        jgPushSendVo.setNotificationTitle(title);
        jgPushSendVo.setMsgTitle(msgTitle);
        jgPushSendVo.setAlias(alias);
        jgPushSendVo.setUrl(url);
        JgPushSendUrl sendUrl = new JgPushSendUrl();
        sendUrl.setUrl(url);
        sendUrl.setType(1);
        jgPushSendVo.setSendUrl(sendUrl);
        return jgPushSendVo;
    }

}
